﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KC.DataAccess.Hub;

namespace KC.Presentation.Mv8CMS
{
	public partial class AffiliatePageEditor : Form
	{
		protected HubDataContext dc = new HubDataContext(System.Configuration.ConfigurationManager.ConnectionStrings["Hub"].ConnectionString);
		protected int PID = -1;

		public AffiliatePageEditor()
		{
			InitializeComponent();
		}
        public AffiliatePageEditor(int PageID)
        {
            PID = PageID;
            InitializeComponent();
            var p = from p2 in dc.Pages
                    where p2.ID == PageID
                    select p2;
            if (p.Count() != 1)
            {
                MessageBox.Show("Can't lock onto Post " + PageID.ToString());
                return;
            }

            Page p3 = p.First();
            txtTitle.Text = p3.Title;
            lblFriendlyName.Text = p3.FriendlyName;
            txtBody.Text = KC.Business.Text.Encoding.BlogEncodeForStorage(p3.Body);

            cbEnabled.Checked = p3.isEnabled;
        }
        private void AffiliatePageEditor_Load(object sender, EventArgs e)
		{
			ddlProgram.DataSource = dc.AffiliatePrograms.OrderBy(p => p.Name);
			ddlCampaign.DataSource = dc.Campaigns.OrderBy(p => p.Title);
		}

		private void btnFriendlyName_Click(object sender, EventArgs e)
		{
			lblFriendlyName.Text = System.Web.HttpUtility.UrlEncode(txtTitle.Text);
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			DoSave();
			Close();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			DialogResult = System.Windows.Forms.DialogResult.Cancel;
			Close();
		}

		private void btnSaveContinue_Click(object sender, EventArgs e)
		{
			DoSave();
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			if (PID == -1) return;
			var p = from p2 in dc.Pages where p2.ID == PID select p2;
			if (p.Count() != 1) return;
			if (MessageBox.Show("Really delete this Affiliate Page?", "Question", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
			{
				try
				{
					dc.Pages.DeleteAllOnSubmit(dc.Pages.Where(p2 => p2.ID == PID));
					dc.SubmitChanges();
					this.DialogResult = DialogResult.Retry;
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.ToString());
				}
				Close();
			}
		}

		private void DoSave()
		{
			// Easy stuff
			Page p = PID > -1
				? (from p2 in dc.Pages where p2.ID == PID select p2).First()
				: new Page();

			p.Title = txtTitle.Text;
			p.Body = KC.Business.Text.Encoding.BlogEncodeForStorage(txtBody.Text);
			p.isEnabled = cbEnabled.Checked;
			p.Keywords = txtKeywords.Text.Replace(";",",").Replace(", ", ",");
			p.CampaignID = ((Campaign)ddlCampaign.SelectedItem).ID;
			p.Description = txtDesc.Text;

			if (PID == -1) p.Created = DateTime.Now;
			if (string.IsNullOrEmpty(p.FriendlyName) || (lblFriendlyName.Text != System.Web.HttpUtility.UrlEncode(txtTitle.Text) && MessageBox.Show("Update FriendlyName?", "Question", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes))
				p.FriendlyName = lblFriendlyName.Text;
			if (PID == -1) dc.Pages.InsertOnSubmit(p);
			
			dc.SubmitChanges();
			PID = p.ID;
		}

		private void ddlCampaign_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (ddlCampaign.SelectedItem == null)
				return;
			ddlProgram.SelectedItem = ((Campaign)ddlCampaign.SelectedItem).AffiliateProgram;
		}

	}
}
